unit OEMMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, FunctionUnit, StdCtrls, ExtCtrls, jpeg;

type
  TfrmOEMEdit = class(TForm)
    Label1: TLabel;
    edtRegisterUrl: TEdit;
    Label2: TLabel;
    edtBrandName: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    btnOpen: TButton;
    btnSave: TButton;
    edtHomepage: TEdit;
    edtTelephone: TEdit;
    edtCompanyName: TEdit;
    edtInstallRegistry: TEdit;
    dlgSave: TSaveDialog;
    dlgOpen: TOpenDialog;
    Label7: TLabel;
    dlgOpenImage: TOpenDialog;
    Panel1: TPanel;
    imgAbout: TImage;
    Label8: TLabel;
    rbNormal: TRadioButton;
    rbNoDisk: TRadioButton;
    Label9: TLabel;
    edtProductName: TEdit;
    Label10: TLabel;
    edtHardInfo: TEdit;
    Label11: TLabel;
    edtMenuTitle: TEdit;
    Label12: TLabel;
    edtOnlineHelpUrl: TEdit;
    Label13: TLabel;
    edtBrandId: TEdit;
    procedure btnSaveClick(Sender: TObject);
    procedure btnOpenClick(Sender: TObject);
    procedure imgAboutDblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmOEMEdit: TfrmOEMEdit;

implementation

uses ConstUnit;

{$R *.dfm}

procedure TfrmOEMEdit.btnSaveClick(Sender: TObject);
var
  SL: TStrings;
  mm: TMemoryStream;
  sm: TStringStream;
  Str: string;
begin
  if not dlgSave.Execute then
    Exit;
  SL := TStringList.Create;
  try

    SL.Values[KEY_OEMRegisterUrl] := Trim(edtRegisterUrl.Text);
    SL.Values[KEY_OEMBrandName] := Trim(edtBrandName.Text);
    SL.Values[KEY_OEMBrandId] := Trim(edtBrandId.Text);
    SL.Values[KEY_OEMHomepage] := Trim(edtHomepage.Text);
    SL.Values[KEY_OEMTelephone] := Trim(edtTelephone.Text);
    SL.Values[KEY_OEMCompanyName] := Trim(edtCompanyName.Text);
    SL.Values[KEY_OEMInstallRegistry] := Trim(edtInstallRegistry.Text);
    SL.Values[KEY_OEMAboutImageFile] := ExtractFileName(imgAbout.Hint);
    SL.Values[KEY_OEMProductName] := Trim(edtProductName.Text);
    SL.Values[KEY_OEMHardInfo] := Trim(edtHardInfo.Text);
    SL.Values[KEY_OEMMenuTitle] := Trim(edtMenuTitle.Text);
    SL.Values[KEY_OEMOnlineHelp] := Trim(edtOnlineHelpUrl.Text);
    if rbNormal.Checked then
      SL.Values[KEY_OEMPlatform] := 'normal'
    else
      SL.Values[KEY_OEMPlatform] := 'nodisk';
//    sm := TStringStream.Create('');
    mm := TMemoryStream.Create;
    try                        
      mm.LoadFromFile(imgAbout.Hint);
      mm.Position := 0;
      SetLength(Str, mm.Size);
      mm.Read(Str[1], mm.Size);
//      sm.CopyFrom(mm, mm.Size);
      SL.Values[KEY_OEMAboutImage] := Str;

    finally
//      sm.Free;
      mm.Free;
    end;


    mm := TMemoryStream.Create;
    try
      SL.SaveToStream(mm);
      CompressStream(mm);
      mm.SaveToFile(dlgSave.FileName);
    finally
      mm.Free;
    end;

  finally
    SL.Free;
  end;
end;

procedure TfrmOEMEdit.btnOpenClick(Sender: TObject);
var
  SL: TStrings;
  mm, umm: TMemoryStream;
  ds: TStringStream;
  fn, Str: string;
  iPos, iLen: Integer;
begin
  if not dlgopen.Execute then
    Exit;

  mm := TMemoryStream.Create;
  umm := TMemoryStream.Create;

  try
    mm.LoadFromFile(dlgOpen.FileName);
    UncompressStream(mm, mm);
    mm.SaveToFile(ChangeFileExt(dlgOpen.FileName, '.txt'));
    SL := TStringList.Create;
    try
      SL.LoadFromStream(mm);
      edtRegisterUrl.Text := SL.Values[KEY_OEMRegisterUrl];
      edtBrandName.Text := SL.Values[KEY_OEMBrandName];
      edtBrandId.Text := SL.Values[KEY_OEMBrandId];
      edtHomepage.Text := SL.Values[KEY_OEMHomepage];
      edtTelephone.Text := SL.Values[KEY_OEMTelephone];
      edtCompanyName.Text := SL.Values[KEY_OEMCompanyName];
      edtInstallRegistry.Text := SL.Values[KEY_OEMInstallRegistry];
      edtProductName.Text := SL.Values[KEY_OEMProductName];
      edtHardInfo.Text := SL.Values[KEY_OEMHardInfo];
      edtMenuTitle.Text := SL.Values[KEY_OEMMenuTitle];
      edtOnlineHelpUrl.Text := SL.Values[KEY_OEMOnlineHelp];
      fn := ExtractFilePath(ParamStr(0)) + SL.Values[KEY_OEMAboutImageFile];

      
      SetLength(Str, mm.Size);
      mm.Position := 0;
      mm.Read(Str[1], mm.Size);
      iPos := Pos(KEY_OEMAboutImage + '=', Str) + Length(KEY_OEMAboutImage + '=');
      iLen := mm.Size - iPos;
      Str := Copy(Str, iPos, iLen);
      umm.Clear;
      umm.Write(Str[1], iLen);
      umm.SaveToFile(fn);
      imgAbout.Picture.LoadFromFile(fn);
      imgAbout.Hint := fn;
      if SL.Values[KEY_OEMPlatform] = 'normal' then
        rbNormal.Checked := True
      else if SL.Values[KEY_OEMPlatform] = 'nodisk' then
        rbNoDisk.Checked := True;
//      ds := TStringStream.Create(SL.Values[KEY_OEMAboutImage]);
//      try
//        ds.Position := 0;
//        umm.CopyFrom(ds, ds.Size);
//        fn := ExtractFilePath(ParamStr(0)) + SL.Values[KEY_OEMAboutImageFile];
//        umm.SaveToFile(fn);
//        imgAbout.Picture.LoadFromFile(fn);
//      finally
//        ds.Free;
//      end;

    finally
      SL.Free;
    end;

  finally
    mm.Free;
    umm.Free;
  end;


end;

procedure TfrmOEMEdit.imgAboutDblClick(Sender: TObject);
begin

  if not dlgOpenImage.Execute then
    Exit;
  imgAbout.Picture.LoadFromFile(dlgOpenImage.FileName);
  imgAbout.Hint := dlgOpenImage.FileName;
end;

end.
